chapter:=$(shell pwd |sed 's/.*\///')

.PHONY: main slides exercises problems debug git clean

main: main.tex
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape main
	cat $(shell ls ../*/*_labels | sed "/${chapter}\/main_labels/d") >> main.aux
	@if [ -e "main.bcf" ]; then biber main;fi
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape main
	grep '^\\newlabel' main.aux > main_labels

once:
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape main
	biber main

slides: main.tex
	cp main.tex main_slides.tex
	sed -i '1s/handout//' main_slides.tex
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape main_slides
	cat $(shell ls ../*/main_slides.aux | sed "/${chapter}/d") >> main_slides.aux
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape main_slides

all:
	for dir in ch*; do \
		cd $${dir}; make exercises git; cd ..; \
	done

snip:
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape snip

print:
	pdfjam --trim '-1cm -1cm -1cm -1cm'  --nup 1x2 --paper b5paper main.pdf

EX:
	export TEXINPUTS=${TEXINPUTS}:./inputs; xelatex --shell-escape exercises.tex
	biber exercises
	export TEXINPUTS=${TEXINPUTS}:./inputs; xelatex --shell-escape exercises.tex

exercises: 
	../inputs/collect exercises.tex temp_exercises.tex
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/exercises.tex
	cat $(shell ls ../*/*_labels | sed "/${chapter}\/exercise_labels/d") >> exercises.aux
	@if [ -e "exercises.bcf" ]; then biber exercises;fi
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/exercises.tex
	grep '^\\newlabel' exercises.aux > exercises_labels

exercises_slides: 
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/exercises_slides.tex
	cat $(shell ls ../*/*_labels | sed "/${chapter}/d") >> exercises_slides.aux
	@if [ -e "exercises_slides.bcf" ]; then biber exercises_slides;fi
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/exercises_slides.tex

problems: 
	../inputs/collect problems.tex temp_problems.tex
	sed -i 's/\\setcounter{exercise}{0}/\\setcounter{exercise}{$(shell grep "^习题总数：" exercises.log | sed "s/习题总数：//")}/' temp_problems.tex
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/problems.tex
	cat $(shell ls ../*/*_labels | sed "/${chapter}\/problems_labels/d") >> problems.aux
	@if [ -e "problems.bcf" ]; then biber problems;fi
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/problems.tex
	grep '^\\newlabel' problems.aux > problems_labels

problems_slides: 
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/problems_slides.tex
	cat $(shell ls ../*/*_labels | sed "/${chapter}/d") >> problems_slides.aux
	@if [ -e "problems_slides.bcf" ]; then biber problems_slides;fi
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/problems_slides.tex


debug:
	# f = file 表示要调试的单个文件
	cp ${f}.tex .debug.tex
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex --shell-escape ../inputs/debug.tex

summary:
	../inputs/summary -s
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex summary.tex

overview:
	../inputs/summary -o
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex overview.tex
	cat ../*/main.aux >> overview.aux
	export TEXINPUTS=${TEXINPUTS}:../inputs; xelatex overview.tex

git:
	@if [ -e main.pdf ]; then cp -u main.pdf ../pdfs/${chapter}.pdf; fi
	@if [ -e main_slides.pdf ]; then cp -u main_slides.pdf ../pdfs/${chapter}_slides.pdf; fi
	@if [ -e exercises.pdf ]; then cp -u exercises.pdf ../pdfs/${chapter}_exercises.pdf; fi
	@if [ -e problems.pdf ]; then cp -u problems.pdf ../pdfs/${chapter}_problems.pdf; fi
	@if [ -e summary.pdf ]; then cp -u summary.pdf ../pdfs/${chapter}_summary.pdf; fi
	@if [ -e overview.pdf ]; then cp -u overview.pdf ../pdfs/${chapter}_overview.pdf; fi
	@if [ -e preview.pdf ]; then cp -u preview.pdf ../pdfs/${chapter}_preview.pdf; fi
	@if [ -e exercises_slides.pdf ]; then cp -u exercises_slides.pdf ../pdfs/${chapter}_exercises_slides.pdf; fi
	@if [ -e problems_slides.pdf ]; then cp -u problems_slides.pdf ../pdfs/${chapter}_problems_slides.pdf; fi
	#@if [ -e problems.pdf ]; then cp -u problems.pdf ../git/${chapter}_problems.pdf; fi
	#cd ../git;./push.sh
	
clean:
	-rm -f -r _minted* *.aux *.bcf *.xml *.out *.toc *.snm *.nav *.log *.vrb *.blg *.bbl *.ptc *.idx debug.pdf temp_*
